package com.customization.jucailinkq.workflow;

import com.engine.core.cfg.annotation.CommandDynamicProxy;
import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.jucailinkq.attendance.workflow.proxy.tactics.WorkFlowHandleTacis;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.workflow.cmd.monitor.DoRepossessedCmd;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.workflow.workflow.RequestForceDrawBack;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 流程强制收回代理类
 */
@CommandDynamicProxy(target = DoRepossessedCmd.class, desc="流程强制收回")
public class KQDoRepossessedProxyCmd extends AbstractCommandProxy<Map<String,Object>> {
    private List<WorkFlowHandleTacis> rulers;
    {
        rulers = new Utils<WorkFlowHandleTacis>().getAllClassByInterface(WorkFlowHandleTacis.class);

    }
    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> command) {

        BaseBean bb = new BaseBean();
        bb.writeLog("KQDoRepossessedProxyCmd start");

        Map<String, Object> resultMap = nextExecute(command);

        String sql = "select ljid,lclx,lcbd  from uf_jcl_kq_kqlcsz where zt=0";
        List<Map<String,Object>> list = DbTools.getSqlToList(sql);
        Map<String,List<Map<String,Object>>> groupByMap = list.stream().collect(Collectors.groupingBy(e->e.get("ljid").toString()));

        RecordSet rs = new RecordSet();
        Map<String, Object> requestMap = (Map<String, Object>)resultMap.get("result");
        bb.writeLog("KQDoRepossessedProxyCmd requestMap"+requestMap.toString());
        requestMap.entrySet().forEach(e->{
            String requestId = e.getKey();
            int status = Integer.valueOf(e.getValue().toString());
            if (status == RequestForceDrawBack.SUCCESS){
                rs.executeProc("workflow_Requestbase_SByID", requestId);
                if (rs.next()) {
                    String workflowid = rs.getString("workflowid");
                    bb.writeLog("KQDoRepossessedProxyCmd workflowid"+workflowid);
                    bb.writeLog("KQDoRepossessedProxyCmd groupByMap"+groupByMap.toString());
                    if (groupByMap.get(workflowid) != null && groupByMap.get(workflowid).size()>0){
                        Map<String,Object> map = groupByMap.get(workflowid).get(0);
                        String workflowType = map.get("lclx").toString();
                        bb.writeLog("KQDoRepossessedProxyCmd workflowType"+workflowType);
                        for (WorkFlowHandleTacis workFlowHandleTacis :rulers){
                            if (workFlowHandleTacis.support(workflowType)){
                                map.put("requestId",requestId);
                                map.put("operate","dorepossesse");
                                workFlowHandleTacis.reset(map);
                            }
                        }
                    }
                }
            }
        });

        return resultMap;
    }
}
